package com.maxd.modules.redPacket.service.impl;

import com.maxd.error.Result;
import com.maxd.error.ResultUtil;
import com.maxd.modules.redPacket.dao.RedPacketDetailsJpaRepository;
import com.maxd.modules.redPacket.dao.RedPacketDetailsRepository;
import com.maxd.modules.redPacket.service.IRedPacketDetailsService;
import com.maxd.modules.redPacket.service.IRedPacketService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  红包助力详细信息
 */
@Service
public class RedPacketDetailsServiceImpl implements IRedPacketDetailsService {

    @Autowired
    private RedPacketDetailsRepository redPacketDetailsRepository;
    @Autowired
    private RedPacketDetailsJpaRepository redPacketDetailsJpaRepository;


    @Override
    public Result selectRedPacketDetailsList(Integer page,Integer size,Long redPacketId){
        Pageable pageable = PageRequest.of(page, size);
        Page<Map<String, Object>> maps = redPacketDetailsRepository.selectRedPacketDetailsList(pageable, redPacketId);
        List<Map<String, Object>> content = maps.getContent();
        List<String> list=new ArrayList<>();
        for(Map<String,Object> map:content){
            String nickName = String.valueOf(map.get("nickName"));
            String phone = String.valueOf(map.get("phone"));
            if(StringUtils.isEmpty(nickName)){
                nickName=phone;
            }
            nickName = nickName.replaceAll("(\\d{3})\\d*([0-9a-zA-Z]{4})","$1****$2");
            list.add(nickName);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("data",maps);
        map.put("list",list);
        return ResultUtil.success(map);
    }




}
